<?php
namespace Service\Controller;
use Think\Controller;
class RoomController extends Controller {

    public function index(){
        if(!S('smartHouse')){
        	$room_db = D('Room');
            $data = $room_db->where(array('unit_id'=>array('neq',10237)))->relation(true)->select();
            // $data = D('Room')->query('select c.id as c_id,c.name as c_name,z.id as z_id,z.name as z_name,b.id as b_id,b.number as b_name,u.id as u_name,u.number as u_name,r.id as r_id,r.number as r_name from house_community as c,house_zone as z,house_building as b,house_unit as u,house_room as r where c.id=r.community_id AND z.id=r.zone_id AND b.id=r.building_id AND u.id=r.unit_id AND u.id <> 10237;');
        	// $this->ajaxReturn($data);
            foreach($data as $k=>$v){
                $c[$v['community_id']]['name'] = $v['community']['name'];

                $z[$v['zone_id']]['name'] = $v['zone']['name'];
                $z[$v['zone_id']]['pid'] = $v['community_id'];

                $b[$v['building_id']]['name'] = $v['building']['number'];
                $b[$v['building_id']]['pid'] = $v['zone_id'];
                $b[$v['building_id']]['ppid'] = $v['community_id'];

                $u[$v['unit_id']]['name'] = $v['unit']['number'];
                $u[$v['unit_id']]['pid'] = $v['building_id'];
                $u[$v['unit_id']]['ppid'] = $v['zone_id'];
                $u[$v['unit_id']]['pppid'] = $v['community_id'];

                $r[$v['id']]['name'] = $v['number'];
                $r[$v['id']]['pid'] = $v['unit_id'];
                $r[$v['id']]['ppid'] = $v['building_id'];
                $r[$v['id']]['pppid'] = $v['zone_id'];
                $r[$v['id']]['ppppid'] = $v['community_id'];
            }

            // foreach($data as $k=>$v){
            //     $c[$v['c_id']]['name'] = $v['c_name'];

            //     $z[$v['z_id']]['name'] = $v['z_name'];
            //     $z[$v['z_id']]['pid'] = $v['c_id'];

            //     $b[$v['b_id']]['name'] = $v['b_name'];
            //     $b[$v['b_id']]['pid'] = $v['z_id'];
            //     $b[$v['b_id']]['ppid'] = $v['c_id'];

            //     $u[$v['u_id']]['name'] = $v['u_name'];
            //     $u[$v['u_id']]['pid'] = $v['b_id'];
            //     $u[$v['u_id']]['ppid'] = $v['z_id'];
            //     $u[$v['u_id']]['pppid'] = $v['c_id'];

            //     $r[$v['r_id']]['name'] = $v['r_name'];
            //     $r[$v['r_id']]['pid'] = $v['u_id'];
            //     $r[$v['r_id']]['ppid'] = $v['b_id'];
            //     $r[$v['r_id']]['pppid'] = $v['z_id'];
            //     $r[$v['r_id']]['ppppid'] = $v['c_id'];
            // }

            foreach($c as $k=>$v){
                $result[$k]['community_id'] = $k;
                $result[$k]['community_name'] = $v['name'];
            }

            foreach($z as $k=>$v){
                $result[$v['pid']]['data'][$k]['zone_id'] = $k;
                $result[$v['pid']]['data'][$k]['zone_name'] = $v['name'];
            }

            foreach($b as $k=>$v){
                $result[$v['ppid']]['data'][$v['pid']]['data'][$k]['building_id'] = $k;
                $result[$v['ppid']]['data'][$v['pid']]['data'][$k]['building_name'] = $v['name'];
            }

            foreach($u as $k=>$v){
                $result[$v['pppid']]['data'][$v['ppid']]['data'][$v['pid']]['data'][$k]['unit_id'] = $k;
                $result[$v['pppid']]['data'][$v['ppid']]['data'][$v['pid']]['data'][$k]['unit_name'] = $v['name'];
            }

            foreach($r as $k=>$v){
                if($result[$v['ppppid']]['data'][$v['pppid']]['data'][$v['ppid']]['data'][$v['pid']]){
                    $result[$v['ppppid']]['data'][$v['pppid']]['data'][$v['ppid']]['data'][$v['pid']]['data'][$k]['room_id'] = $k;
                    $result[$v['ppppid']]['data'][$v['pppid']]['data'][$v['ppid']]['data'][$v['pid']]['data'][$k]['room_name'] = $v['name'];
                }else{
                    $result[$v['ppppid']]['data'][$v['pppid']]['data'][$v['ppid']]['data']['0']['unit_id'] = 0;
                    $result[$v['ppppid']]['data'][$v['pppid']]['data'][$v['ppid']]['data']['0']['unit_name'] = '无单元';
                    $result[$v['ppppid']]['data'][$v['pppid']]['data'][$v['ppid']]['data']['0']['data'][$k]['room_id'] = $k;
                    $result[$v['ppppid']]['data'][$v['pppid']]['data'][$v['ppid']]['data']['0']['data'][$k]['room_name'] = $v['name'];
                }
            }

            S('smartHouse',$result);
        }
        header('Access-Control-Allow-Origin: *');
        $res['code'] = 200;
        $res['data'] = S('smartHouse');
        $this->ajaxReturn($res);
    }

}